package com.zhoug.common.share.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.zhoug.common.R;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 分享窗口数据适配器
 * @Author 35574
 * @Date 2021/10/14
 * @Description
 */
class ShareAdapter extends RecyclerView.Adapter<ShareAdapter.VH> implements View.OnClickListener{
    private List<ShareBean> mData;
    private OnItemClickListener onItemClickListener;

    public ShareAdapter(Context context) {

    }

    public void setData(List<ShareBean> data) {
        this.mData = data;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.common_share_dialog_list_item, parent, false);

        return new VH(view);
    }

    @Override
    public int getItemCount() {
        return mData != null ? mData.size() : 0;
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        ShareBean shareBean = mData.get(position);
        holder.icon.setImageResource(shareBean.getImageId());
        holder.name.setText(shareBean.getText());

        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if(onItemClickListener!=null){
            int pos= (int) v.getTag();
            ShareBean shareBean = mData.get(pos);
            onItemClickListener.onItemClick(this,shareBean,pos);
        }
    }

    static class VH extends RecyclerView.ViewHolder {
        private ImageView icon;
        private TextView name;

        public VH(View itemView) {
            super(itemView);
            icon = itemView.findViewById(R.id.iv_share_icon);
            name = itemView.findViewById(R.id.tv_share_name);
        }
    }

    public interface OnItemClickListener{
        void onItemClick(ShareAdapter adapter, ShareBean item, int position);
    }

}
